%%%%Write the classifier accuracy and score matlab matrices to afni BRIKS:
%%%Can do this for avg_accuracy, avg_score, and trial-by-trial scores and
%%%guesses for each conditionfull_mask_fewzeros.desisl_',s,'+orig

%%%%TO RUN! LOAD MATLAB/R2009a!!!!!!

function desisl_mvpa_writetoafni(sub)


%create an array of subject numbers 
%subnums=[2:12,14:20,22:37,39,40];

model='B21_obj_disp';
filename = 'B21_obj_disp';
radius = 'r2'; %make sure to call appropriate createMetaFromMask function (_r2 = radius of 2)
rad = 2;
classifier = 'svm_MATLAB';%assumes no correlation among voxels
%classifier = 'lda_shrinkage';%assumes some correlation among voxels and that that correlation is the same across conditions
%classifier = 'svm_linear'
smooth='6';
nObjs = 2;
 
if sub < 10
    s = ['0',num2str(sub)]
else s = num2str(sub)
end

subj = init_subj('desisl', sprintf(['s', s]));

cd (['~/../../ncf/cushman/Regan/desert_island/desisl_',s,'/desisl_',s,'.results']);

%Load mask
subj = load_afni_mask(subj, 'WB', ['full_mask_fewzeros.desisl_',s,'+orig']);
mask=get_mat(subj, 'mask', 'WB');

%Load folds & regressors
load (['~/../../ncf/cushman/Regan/desert_island/desisl_',s,'/desisl_',s,'.results/s',s,'_mvpa_params.mat']);

for cur_obj=1:nObjs
    if cur_obj==1
        labels=lv_labels;
        conds=lv_conds;
        obj='lv'
    else
        labels=hv_labels;
        conds=hv_conds;
        obj='hv'
    end
       
%orient labels and conds with data and runs    
labels=labels';
conds=conds';

%{
for cur_cond=1:max(conds)

    if cur_cond==1
        c='targ_had'
    else if cur_cond==2
            c='targ_did'
        else if cur_cond==3
                c='third_had'
            else
                c='third_did'
            end
        end
    end
    %}
c='allconds';

    %Load trial-by-trial classification accuracies and scores
    %load(['~/../../ncf/cushman/Regan/desert_island/desisl_',s,'/desisl_',s,'.results/',model,'_',obj,'_',c,'_full_scores_',classifier,'.mat'],'full_scores')
    %load(['~/../../ncf/cushman/Regan/desert_island/desisl_',s,'/desisl_',s,'.results/',obj,'_',c,'_full_guesses_',classifier,'.mat'],'full_guesses')

    %Load average classification accuracies and scores
    %load(['~/../../ncf/cushman/Regan/desert_island/desisl_',s,'/desisl_',s,'.results/',model,'_',obj,'_',c,'_avg_accuracy_',classifier,'.mat'],'avg_accuracy')
    load(['~/../../ncf/cushman/Regan/desert_island/desisl_',s,'/desisl_',s,'.results/BLOCK_31_',obj,'_',c,'_avg_accuracy_',classifier,'.mat'],'avg_accuracy')
    %load(['~/../../ncf/cushman/Regan/desert_island/desisl_',s,'/desisl_',s,'.results/',model,'_',obj,'_',c,'_avg_score_',classifier,'.mat'],'avg_score')


%insert accuracy data back into our subject structure and write to AFNI
subj = initset_object(subj, 'pattern', ['desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_avg_accuracy'], avg_accuracy', 'masked_by', 'WB');
cd(['/ncf/cushman/Regan/desert_island/desisl_',s,'/desisl_',s,'.results'])   
write_to_afni(subj, 'pattern', ['desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_avg_accuracy'], ['full_mask_fewzeros.desisl_',s,'+orig'])

%insert average score data back into subject structure and write to AFNI
%subj = initset_object(subj, 'pattern', ['desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_avg_score'], avg_score', 'masked_by', 'WB');
%cd(['/ncf/cushman/Regan/desert_island/desisl_',s,'/desisl_',s,'.results'])   
%write_to_afni(subj, 'pattern', ['desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_avg_score'], ['full_mask_fewzeros.desisl_',s,'+orig'])
%{
%insert trial-by-trial scores back into subject structure and write to AFNI  
[trash,trials]=size(full_scores)
for t=1:trials
    
    %loop through every trial and set temp_posts to the column that is just
    %for that trial
    temp_scores=full_scores(:,t);
    %temp_guesses=full_guesses(:,t);
    
    %write just this trial's data to afni
    subj = initset_object(subj, 'pattern', ['desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_temp_scores',num2str(t)], temp_scores, 'masked_by', 'WB');  
    write_to_afni(subj, 'pattern', ['desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_temp_scores',num2str(t)], ['full_mask_fewzeros.desisl_',s,'+orig']);

    %subj = initset_object(subj, 'pattern', ['desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_temp_guesses',num2str(t)], temp_guesses, 'masked_by', 'WB');  
    %write_to_afni(subj, 'pattern', ['desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_temp_guesses',num2str(t)], ['full_mask_fewzeros.desisl_',s,'+orig']);

    if t==1
        %if it is the first trial create a new full posts afni map
        unix(sprintf(['3dcopy desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_temp_scores',num2str(t),'+orig desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_full_scores+orig']));
        %unix(sprintf(['3dcopy desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_temp_guesses',num2str(t),'+orig desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_full_guesses+orig']));
    else
        %otherwise add the new temp_posts map as an additional sub-brick in
        %the full_posts BRIK
        unix(sprintf(['3dTcat -glueto desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_full_scores+orig desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_temp_scores',num2str(t),'+orig']));
        %unix(sprintf(['3dTcat -glueto desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_full_guesses+orig desisl_',s,'_',classifier,'_',radius,'_',filename,'_',obj,'_',c,'_temp_guesses',num2str(t),'+orig']));
    end
end
%}
%unix(sprintf('rm *temp_scores*'));
%unix(sprintf('rm *temp_guesses*'));
end
end
